package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
   @Autowired
    private DishMapper dishMapper;
   @Autowired
   private DishFlavorMapper dishFlavorMapper;
   @Autowired
   private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional
    @Override
//    TODO 看注解 和逻辑
    public void add(DishDTO dishDTO) {
      Dish dish=new Dish();
      BeanUtils.copyProperties(dishDTO,dish);
      dishMapper.add(dish);

      Long id = dish.getId(); //和xml里面的注解有关系--才可以获得的

      List<DishFlavor> flavors = dishDTO.getFlavors();

      if(flavors.size()>0 && flavors!=null) {
          flavors.forEach(dishFlavor -> {
              dishFlavor.setDishId(id);
          });
          dishFlavorMapper.add(flavors);
      }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page page = dishMapper.page(dishPageQueryDTO);
        PageResult p=new PageResult(page.getTotal(),page.getResult());
        return  p;
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deletes(List<Long> ids) {
//        起售否？
        for (Long id : ids) {
            Dish byid = dishMapper.Byid(id);
            if(byid.getStatus()== StatusConstant.ENABLE) {
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
//      关联套餐没？
        List<Long> setmealIdsByDishIDs = setmealDishMapper.getSetmealIdsByDishIDs(ids);
        if(setmealIdsByDishIDs !=null && setmealIdsByDishIDs.size()>0) {
            throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//        菜品的删除
        dishMapper.deletes(ids);
//        菜品口味的删除
        ids.forEach(id->{
            dishFlavorMapper.delets(id);
        });



    }

    /**
     * 根据菜品id查询菜品  用于数据回显
     * @param id
     * @return
     */
    @Override
    public DishVO findAllByid(Long id) {
//        口味的查询
        List<DishFlavor> flavor = dishFlavorMapper.findAllByid(id);
//        菜品的查询
        Dish byid = dishMapper.Byid(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(byid,dishVO);
        dishVO.setFlavors(flavor);
        return dishVO;
    }

    /**
     * 修改菜品和口味
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
//        修改了菜品的基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateWithFlavor(dish);
//        删除口味信息
        dishFlavorMapper.delets((dishDTO.getId()));
//        按照本次的修改添加口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors.size()>0 && flavors!=null) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.add(flavors);
        }

    }

    /**
     * 起售 停售
     * @param status
     * @param id
     */
    @Override
    public void StarorStop(Integer status, Long id) {
        Dish build = new Dish().builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.updateWithFlavor(build);

    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> BycategoryidFindALL(Integer categoryId) {
        List<Dish> dishes = dishMapper.BycategoryidFindALL(categoryId);
        return dishes;
    }

    /**
     * APP端根据分类ID 和起售 查询菜品
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);
        List<DishVO> dishVOList=new ArrayList<>();
        dishList.forEach(d -> {

            DishVO dishVO=new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            List<DishFlavor> allByid = dishFlavorMapper.findAllByid(d.getId());
            dishVO.setFlavors(allByid);
            dishVOList.add(dishVO);
        });
        return  dishVOList;
    }
}
